/**
 * Simple regex experiment to create an automatic emoticons by Taufik Nurrohman
 * Visit: http://hompimpaalaihumgambreng.blogspot.com
 * Update: 23 August 2012
 */

$(function() {

	// Append an emoticon bar before comment-form
	if (putEmoAbove) {
		$(putEmoAbove).before('<div class="emoWrap"> :gitar :) :haha ^_^ :D   @@, :-bd  :call: :&#39;(  :&#92;  B) :Q  7:(    &lt;3 </div>');
	}
	function emo(emo, imgRep, emoKey) {
		$(emoRange).each(function() {
			$(this).html($(this).html().replace(/<br ?\/?>(:|;|=|\^)/ig, "<br> $1").replace(emo, " <img src='" + imgRep + "' class='emo delayLoad' alt='" + emoKey + "' />"));
		});
	}
	emo(/\s:\)+/g, "https://simple-cursor.googlecode.com/svn/trunk/ckckck.gif", ":)");
	emo(/\s:\gitar/g, "http://simple-cursor.googlecode.com/svn/trunk/gitar.gif", ":gitar");
	emo(/\s@@,/g, "https://simple-cursor.googlecode.com/svn/trunk/master.gif", "@@,");
	emo(/\s:call:/ig, "https://simple-cursor.googlecode.com/svn/trunk/menelpon.gif", ":call:");
	emo(/\s:haha/ig, "https://simple-cursor.googlecode.com/svn/trunk/ngakak.gif", ":haha");
	emo(/\s:\\/g, "https://simple-cursor.googlecode.com/svn/trunk/tepuktangan.gif", ":&#92;");
	emo(/\s:D/g, "https://simple-cursor.googlecode.com/svn/trunk/taxedo.gif", ":D");
	emo(/\s\^(\_|)\^/g, "http://simple-cursor.googlecode.com/svn/trunk/ngeledek.gif", "^_^");
	emo(/\s:'\(/g, "https://simple-cursor.googlecode.com/svn/trunk/no-no.gif", ":&#39;(");
	emo(/\sB\)/g, "https://simple-cursor.googlecode.com/svn/trunk/boy.gif", "B)");
	emo(/\s:Q/ig, "https://simple-cursor.googlecode.com/svn/trunk/cape.gif", ":Q");
	emo(/\s7:\(/g, "https://simple-cursor.googlecode.com/svn/trunk/80.gif", "7:(");
	emo(/\s7:O/ig, "https://simple-cursor.googlecode.com/svn/trunk/menelpon.gif", "7:O");
	emo(/\s&lt;3/ig, "https://simple-cursor.googlecode.com/svn/trunk/menangis.gif", "&amp;amp;lt;3");
	emo(/\s:-bd/ig, "http://karina-project.googlecode.com/files/profilk.gif", ":-bd");
	var one = 0; // Show alert one times!

	// Click anywhere to hide the emoticon
	$(document.body).on("click", function() {
		$('.emoKey').remove();
	});

	// Click to show the code!
	$('.emo').css('cursor', 'pointer').on("click", function(e) {
		$('.emoKey').remove();
		$(this).after('<input class="emoKey" type="text" size="6" value=" ' + this.alt + '" />');
		$('.emoKey').trigger("select");
		if(emoMessage && one === 0) {
			alert(emoMessage);
			one = 1;
		}
		e.stopPropagation();
	});

});
